package com.df.springmvc.system.log.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.impl.Log4JLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.bean.DhtmlGridBean;
import com.common.bean.DhtmlGridFieldBean;
import com.common.util.SN_GUID;
import com.common.util.DhtmlGrid.DhtmlGridAttribuate;
import com.df.springmvc.supper.SupperServices;
import com.df.springmvc.system.log.dao.LogDao;

/**
 * 本类只做数据运算处理
 * @author David
 */
@Service
public class LogServices extends SupperServices{
	@Autowired
	LogDao dao;
	
	public List<Object> findAllEmployees(){
		// TODO Auto-generated method stub
		return null; 
	}
	/**
	 *数据查询
	 */
	public DhtmlGridBean query(DhtmlGridBean gridBean,HashMap<String,String> param_HashMap) throws Exception{
		// TODO Auto-generated method stub
		Object[] attribute=new Object[]{};
		gridBean.cleanSessionXmlFile();
		DhtmlGridAttribuate attribuate=new DhtmlGridAttribuate();
		attribuate.addFieldBean(new DhtmlGridFieldBean("LOGID","<center></center>",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("MODUAL","操作模块",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("CONTENT","操作说明",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("USERNAME","<center></center>",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("staffName","操作人",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("CREATETIME","操作时间",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		/**
		 * 增加对应的业务代码
		 */
		ArrayList<Object> list=null;
		gridBean.setAttribuate(attribuate);
		list=dao.query(param_HashMap,attribute,0,2000);
		for(int i=0;i < list.size();i++){
			gridBean.addData(list.get(i));
			if((i + 1) % gridBean.getPageSize() == 0){
				gridBean.createSessionXmlFile();
			}
		}
		gridBean.createSessionXmlFile();
		return gridBean;
	}
	/**
	 *添加数据
	 *paramMap需要传入的参数：TYPE/MODUAL/FUNC/CONTENT
	 */
	@Override
	public Object add(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		HashMap<String,Object>  columnMap = new HashMap<String,Object>(paramMap);
		//装载日志参数
		String orgCode = SN_GUID.getUUID_Num();
		columnMap.put("guid",SN_GUID.getUUID());
		columnMap.put("status","1");
		columnMap.put("createby",getCurrentUserBean().getUserId());
		columnMap.put("createtime",new Date());
		
		columnMap.put("LOGID",SN_GUID.getUUID());
		columnMap.put("USERNAME",getCurrentUserBean().getLoginName());
		columnMap.put("LOGTIME",new Date());
		Object addResult = dao.add(columnMap);
		if(Integer.parseInt(addResult.toString())>0){
			return "添加日志成功";
		}else{
			return "添加日志失败";
		}
	}
	/**
	 * 添加数据
	 * @param USERNAME
	 * @param LOGTIME
	 * @param TYPE
	 * @param MODUAL
	 * @param FUNC
	 * @param CONTENT
	 * @return
	 * @throws Exception
	 */
	public Object add(String TYPE,String MODUAL,String FUNC,String CONTENT) throws Exception{
		// TODO Auto-generated method stub
		HashMap<String,Object>  columnMap = new HashMap<String,Object>();
		//装载日志参数
		String orgCode = SN_GUID.getUUID_Num();
		columnMap.put("guid",SN_GUID.getUUID());
		columnMap.put("status","1");
		columnMap.put("createby",getCurrentUserBean().getUserId());
		columnMap.put("createtime",new Date());
		
		columnMap.put("LOGID",SN_GUID.getUUID());
		columnMap.put("USERNAME",getCurrentUserBean().getLoginName());
		columnMap.put("LOGTIME",new Date());
		columnMap.put("TYPE",TYPE);
		columnMap.put("MODUAL",MODUAL);
		columnMap.put("FUNC",FUNC);
		columnMap.put("CONTENT",CONTENT);
		Object addResult = dao.add(columnMap);
		if(Integer.parseInt(addResult.toString())>0){
			return "添加日志成功";
		}else{
			return "添加日志失败";
		}
	}
	/**
	 * 添加数据
	 * @param currentUser
	 * @param TYPE
	 * @param MODUAL
	 * @param FUNC
	 * @param CONTENT
	 * @return
	 * @throws Exception
	 */
	public Object add(String currentUser,String TYPE,String MODUAL,String FUNC,String CONTENT) throws Exception{
		// TODO Auto-generated method stub
		HashMap<String,Object>  columnMap = new HashMap<String,Object>();
		//装载日志参数
		columnMap.put("guid",SN_GUID.getUUID());
		columnMap.put("status","1");
		columnMap.put("createby",currentUser);
		columnMap.put("createtime",new Date());
		
		columnMap.put("LOGID",SN_GUID.getUUID());
		columnMap.put("USERNAME",currentUser);
		columnMap.put("LOGTIME",new Date());
		columnMap.put("TYPE",TYPE);
		columnMap.put("MODUAL",MODUAL);
		columnMap.put("FUNC",FUNC);
		columnMap.put("CONTENT",CONTENT);
		Object addResult = dao.add(columnMap);
		if(Integer.parseInt(addResult.toString())>0){
			return "添加日志成功";
		}else{
			return "添加日志失败";
		}
	}
	/**
	 *单条数据查询
	 */
	@Override
	public HashMap<String,Object> view(String guid) throws Exception{
		// TODO Auto-generated method stub
		return dao.view(guid);
	}
	
}
